using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;


namespace DAL
{
   public class tbtinhtrangsanphamDAL
    {
        private SqlConnection cn = null;
        public tbtinhtrangsanphamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public tbtinhtrangsanphamDAL(tbtinhtrangsanphamDAL thach)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
       public void Insert(tbtinhtrangsanpham tinhtrangsanpham)
        {
            SqlCommand cm = new SqlCommand("tinhtrangsanpham_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tentinhtrangsanpham", tinhtrangsanpham.Tentinhtrangsanpham);
            cm.ExecuteNonQuery();
        }
       public void Update(tbtinhtrangsanpham tinhtrangsanpham)
        {
            SqlCommand cm = new SqlCommand("tinhtrangsanpham_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matinhtrangsanpham", tinhtrangsanpham.Matinhtrangsanpham);
            cm.Parameters.AddWithValue("@tentinhtrangsanpham", tinhtrangsanpham.Tentinhtrangsanpham);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tinhtrangsanpham_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matinhtrangsanpham", iD);
            cm.ExecuteNonQuery();
        }
       public tbtinhtrangsanphamcollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tinhtrangsanpham_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbtinhtrangsanphamcollection tinhtrangsanpham1Collection = new tbtinhtrangsanphamcollection();
            while (dr.Read())
            {
                tbtinhtrangsanpham tinhtrangsanpham = new tbtinhtrangsanpham();
                tinhtrangsanpham.Matinhtrangsanpham = int.Parse(dr.GetValue(0).ToString());
                tinhtrangsanpham.Tentinhtrangsanpham = dr.GetValue(1).ToString();
                tinhtrangsanpham1Collection.Add(tinhtrangsanpham);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tinhtrangsanpham1Collection;
        }
    }
}
